//
//  WLAlertView.m
//  newShop
//
//  Created by scjh on 15/5/20.
//  Copyright (c) 2015年 wangliang0. All rights reserved.
//

#import "WLAlertView.h"

@implementation WLAlertView


+ (instancetype)share {
    
    static dispatch_once_t once;
    static WLAlertView *alertView = nil;
    dispatch_once(&once, ^{
        
        alertView = [[WLAlertView alloc] init];
        
    });
    return alertView;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        CGRect frame = [UIApplication sharedApplication].keyWindow.frame;
        self.frame = frame;
        [self UI];
    }
    return self;
}

- (void) UI{
    
    UIView *hiddenView = [[UIView alloc] initWithFrame:self.bounds];
    hiddenView.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5];
    [self addSubview:hiddenView];
    
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    [hiddenView addGestureRecognizer:tapGesture];
}

- (void)tapGestureAction:(UITapGestureRecognizer *)gesture {
    
    [WLAlertView dismiss];
}

+ (void)showWith:(UIView *)showView withCenterStatus:(CenterStatus)centerStatus;  {
    
    [[WLAlertView share] addSubview:showView];
    switch (centerStatus) {
        case CenterStatusMiddle:
            showView.center = [WLAlertView share].center;
            break;
        case CenterStatusDown:
            showView.center =
            CGPointMake(
            CGRectGetWidth([WLAlertView share].frame) / 2,
            CGRectGetHeight([WLAlertView share].frame) - CGRectGetHeight(showView.frame) / 2
            );
            break;
            
        default:
            break;
    }
    [[UIApplication sharedApplication].keyWindow addSubview:[WLAlertView share]];
}

+ (void)dismiss {
    
    UIView *view = [WLAlertView share].subviews[1];
    [view removeFromSuperview];
    view = nil;
    [[WLAlertView share] removeFromSuperview];
}

@end
